package cn.android666.ping

import android.content.Context
import androidx.core.content.edit

/** 持久化变量，会保存到配置文件中，也可当全局变量使用 */
object PersistentVariables {
    private const val SERVER_IP = "serverIp"
    private const val SERVER_PORT = "serverPort"
    private const val TIME_MILLIS = "timeMillis"
    private const val PING_COMMAND = "pingCommand"
    private const val DESTINATION = "destination"
    private const val PACKAGE_SIZE = "packageSize"
    private const val INTERVAL = "interval"
    private const val DEADLINE = "deadline"
    private const val COUNT = "count"
    private const val TIMEOUT = "timeout"
    private const val IS_CHECKED_DEADLINE = "isCheckedDeadline"
    private const val IS_CHECKED_COUNT = "isCheckedCount"
    private const val IS_CHECKED_TIMEOUT = "isCheckedTimeout"
    private const val IS_CHECKED_DONT_FRAGMENT = "isCheckedDontFragment"
    private const val IS_CHECKED_MANUAL_EDIT_COMMAND = "isCheckedManualEditCommand"

    private val sp = App.app.getSharedPreferences("config", Context.MODE_PRIVATE)
    @Suppress("SameParameterValue")
    private fun get(key: String, defaultValue: String): String = sp.getString(key, defaultValue) ?: ""
    private fun get(key: String, defaultValue: Int): Int = sp.getInt(key, defaultValue)
    private fun get(key: String, defaultValue: Boolean): Boolean = sp.getBoolean(key, defaultValue)
    @Suppress("SameParameterValue")
    private fun put(key: String, value: String) { sp.edit { putString(key, value) } }
    private fun put(key: String, value: Int) { sp.edit { putInt(key, value) } }
    private fun put(key: String, value: Boolean) { sp.edit { putBoolean(key, value) } }

    var serverIp: String = ""
        get() {
            if (field.isBlank()) {
                field = get(SERVER_IP, "175.178.213.199")
            }
            return field
        }
        set(value) {
            field = value
            put(SERVER_IP, value)
        }

    var serverPort: Int = -1
        get() {
            if (field == -1) {
                field = get(SERVER_PORT, 6601)
            }
            return field
        }
        set(value) {
            field = value
            put(SERVER_PORT, value)
        }

    var timeMillis: Int = -1
        get() {
            if (field == -1) {
                field = get(TIME_MILLIS, 10)
            }
            return field
        }
        set(value) {
            field = value
            put(TIME_MILLIS, value)
        }

    var pingCommand: String = ""
        get() {
            if (field.isBlank()) {
                field = get(PING_COMMAND, "")
            }
            return field
        }
        set(value) {
            field = value
            put(PING_COMMAND, value)
        }

    var destination: String = ""
        get() {
            if (field.isBlank()) {
                field = get(DESTINATION, "")
            }
            return field
        }
        set(value) {
            field = value
            put(DESTINATION, value)
        }

    var packageSize: String = ""
        get() {
            if (field.isBlank()) {
                field = get(PACKAGE_SIZE, "")
            }
            return field
        }
        set(value) {
            field = value
            put(PACKAGE_SIZE, value)
        }

    var interval: String = ""
        get() {
            if (field.isBlank()) {
                field = get(INTERVAL, "")
            }
            return field
        }
        set(value) {
            field = value
            put(INTERVAL, value)
        }

    var deadline: String = ""
        get() {
            if (field.isBlank()) {
                field = get(DEADLINE, "")
            }
            return field
        }
        set(value) {
            field = value
            put(DEADLINE, value)
        }

    var count: String = ""
        get() {
            if (field.isBlank()) {
                field = get(COUNT, "")
            }
            return field
        }
        set(value) {
            field = value
            put(COUNT, value)
        }

    var timeout: String = ""
        get() {
            if (field.isBlank()) {
                field = get(TIMEOUT, "")
            }
            return field
        }
        set(value) {
            field = value
            put(TIMEOUT, value)
        }

    var isCheckedDeadline: Boolean = false
        get() {
            if (!field) {
                field = get(IS_CHECKED_DEADLINE, false)
            }
            return field
        }
        set(value) {
            field = value
            put(IS_CHECKED_DEADLINE, value)
        }

    var isCheckedCount: Boolean = false
        get() {
            if (!field) {
                field = get(IS_CHECKED_COUNT, false)
            }
            return field
        }
        set(value) {
            field = value
            put(IS_CHECKED_COUNT, value)
        }

    var isCheckedTimeout: Boolean = false
        get() {
            if (!field) {
                field = get(IS_CHECKED_TIMEOUT, false)
            }
            return field
        }
        set(value) {
            field = value
            put(IS_CHECKED_TIMEOUT, value)
        }

    var isCheckedDontFragment: Boolean = false
        get() {
            if (!field) {
                field = get(IS_CHECKED_DONT_FRAGMENT, false)
            }
            return field
        }
        set(value) {
            field = value
            put(IS_CHECKED_DONT_FRAGMENT, value)
        }

    var isCheckedManualEditCommand: Boolean = false
        get() {
            if (!field) {
                field = get(IS_CHECKED_MANUAL_EDIT_COMMAND, false)
            }
            return field
        }
        set(value) {
            field = value
            put(IS_CHECKED_MANUAL_EDIT_COMMAND, value)
        }

}